home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / CMPLTPAS / GETDIR.PAS < prev    next >
Pascal/Delphi Source File  |  1988-07-14  |  7KB  |  151 lines

  1. {->>>>GetDirectory<<<<-----------------------------------------}
  2. {                                                              }
  3. { Filename: GETDIR.SRC -- Last modified 7/2/88                 }
  4. {                                                              }
  5. { This routine returns a pointer to a linked list of type      }
  6. { DIRRec, which must have been previously defined this way,    }
  7. { along with pointer type DIRPtr to point to it:               }
  8. {                                                              }
  9. { DIRPtr = ^DIRRec;                                            }
  10. { DIRRec = RECORD                                              }
  11. {            FileName  : String15;                             }
  12. {            Attrib    : Byte;                                 }
  13. {            FileSize  : LongInt;                              }
  14. {            TimeStamp : TimeRec;                              }
  15. {            DateStamp : DateRec;                              }
  16. {            Prior     : DIRPtr;                               }
  17. {            Next      : DIRPtr;                               }
  18. {          END;                                                }
  19. {                                                              }
  20. { The linked list will contain a record for every file in the  }
  21. { current directory.  Since the linked list is out in heap,    }
  22. { your directory data takes up NO space in your data segment.  }
  23. { If there are no files in the current directory, the pointer  }
  24. { returned is equal to NIL.                                    }
  25. {                                                              }
  26. { The types TimeRec and DateRec must be defined prior to using }
  27. { GetDirectory.  String80 & DTAToDIR must be defined as well.  }
  28. {                                                              }
  29. {     From: COMPLETE TURBO PASCAL 5.0  by Jeff Duntemann       }
  30. {    Scott, Foresman & Co., Inc. 1988   ISBN 0-673-38355-5     }
  31. {--------------------------------------------------------------}
  32.  
  33. PROCEDURE GetDirectory(Filespec   : String80;
  34.                        Sorted     : Boolean;
  35.                        SortOnName : Boolean;
  36.                        VAR Ascending  : DIRPtr;
  37.                        VAR Descending : DIRPtr);
  38.  
  39. TYPE
  40.   String9 = String[9];
  41.  
  42. VAR
  43.   I         : Integer;
  44.   FindError : Integer;
  45.   Regs      : Registers;
  46.   OurDTA    : SearchRec;
  47.   Root      : DIRPtr;
  48.   Current   : DIRPtr;
  49.   Last      : DIRPtr;
  50.   Holder    : DIRPtr;
  51.   PositionFound : Boolean;
  52.  
  53.  
  54. FUNCTION LaterThan(LeftEntry,RightEntry : DirPtr) : Boolean;
  55.  
  56. BEGIN
  57.   IF LeftEntry^.DateStamp.DateComp > RightEntry^.DateStamp.DateComp THEN
  58.     LaterThan := True
  59.   ELSE
  60.     IF (LeftEntry^.DateStamp.DateComp = RightEntry^.DateStamp.DateComp)
  61.         AND
  62.        (LeftEntry^.TimeStamp.TimeComp > RightEntry^.TimeStamp.TimeComp)
  63.     THEN LaterThan := True
  64.     ELSE LaterThan := False
  65. END;
  66.  
  67.  
  68. PROCEDURE AppendToEnd(VAR Holder,Descending : DIRPtr);
  69.  
  70. BEGIN
  71.   Descending^.Next := Holder;    { Add record to end of list }
  72.   Descending^.Next^.Prior := Descending;  { Set reverse pointer }
  73.   Descending := Descending^.Next;   { Bump Current to next record }
  74. END;
  75.  
  76.  
  77.  
  78. BEGIN  { GetDir }
  79.   FindFirst(FileSpec,$16,OurDTA);      { Make FIND FIRST DOS call... }
  80.   FindError := DOSError;
  81.   IF FindError = 2 THEN  { No files found to match FileSpec }
  82.     BEGIN
  83.       Ascending := NIL;     { Both linked list pointers are NIL }
  84.       Descending := NIL
  85.     END
  86.   ELSE                   { There was at least one file found, so... }
  87.     BEGIN
  88.       New(Root);            { Create a record for the first find }
  89.       DTAtoDIR(Root^);      { Convert first find to DIR format }
  90.       Current := Root;      { The current record is now the root record }
  91.       Descending := Root;   { And also the last record in the list! }
  92.       IF FindError <> 18 THEN
  93.         REPEAT
  94.           FindNext(OurDTA);       { Make FIND NEXT DOS call }
  95.           FindError := DOSError;
  96.           IF FindError <> 18 THEN { More entries exist }
  97.             BEGIN
  98.               New(Holder);        { Create a record with temporary pointer }
  99.               DTAtoDIR(Holder^);  { Convert additional finds to DIR format }
  100.               { Sorted and unsorted lists are constructed differently. }
  101.               { If we're building a sorted list we have to scan it for }
  102.               { each entry to find the proper place in the list.  For  }
  103.               { unsorted lists we just hang the latest found entry on  }
  104.               { the end of the list and bump Current to the Next. }
  105.               IF Sorted THEN
  106.                 BEGIN
  107.                   Current := Root;    { Traverse list to find insert spot: }
  108.                   REPEAT
  109.                     IF SortOnName THEN     { To sort list on file name }
  110.                       IF Current^.FileName > Holder^.FileName THEN
  111.                         PositionFound := True ELSE PositionFound := False
  112.                     ELSE                   { To sort list by time/date }
  113.                       IF LaterThan(Current,Holder) THEN
  114.                         PositionFound := True ELSE PositionFound := False;
  115.                     IF NOT PositionFound THEN
  116.                       Current := Current^.Next;  { Bump to next item }
  117.                   UNTIL (Current = NIL) OR PositionFound;
  118.                   { When PositionFound becomes True, the record needs }
  119.                   { to be inserted in the list BEFORE Current^-- }
  120.                   { This needs to be done differently if Current^ is }
  121.                   { at the head of the list. (i.e., Current = Root)}
  122.                   IF PositionFound THEN  { Insert at beginning... }
  123.                     BEGIN                { ...or in the middle somewhere }
  124.                       { NOTE:  DO NOT change the order of the }
  125.                       { pointer assignments in the following }
  126.                       { IF/THEN/ELSE statement!! }
  127.                       IF Current = Root THEN  { Insert at beginning }
  128.                         BEGIN
  129.                           Holder^.Next := Root;
  130.                           Current^.Prior := Holder;
  131.                           Root := Holder;
  132.                         END
  133.                       ELSE    { Insert in the middle: }
  134.                         BEGIN
  135.                           Holder^.Next  := Current;
  136.                           Holder^.Prior := Current^.Prior;
  137.                           Current^.Prior^.Next := Holder;
  138.                           Current^.Prior := Holder
  139.                         END
  140.                     END
  141.                   ELSE  { The new record belongs at the end of the list }
  142.                     AppendToEnd(Holder,Descending)
  143.                 END
  144.               ELSE   { If no sort, we add the record to the end of the list: }
  145.                 AppendToEnd(Holder,Descending)
  146.             END
  147.         UNTIL FindError = 18;
  148.       Ascending := Root
  149.     END
  150. END;  {GetDirectory}
  151.